package service

import (
	"errors"
	"github.com/cn-ygf/go-admin/internal/modular/system/dao"
	"github.com/cn-ygf/go-admin/internal/modular/system/model"
	"github.com/cn-ygf/go-admin/internal/modular/system/vo"
)

var (
	DictTypeService = new(dictTypeService)
)

var (
	DictTypeNameUniqueError = errors.New("字典名称已存在")
)

// dictTypeService 字典类型管理
type dictTypeService struct {
}

func (s dictTypeService) GetList() ([]*model.SysDictType, error) {
	return dao.SysDictType.Where(dao.SysDictType.Status.Eq("0")).Find()
}

// QueryList 分页查询字典类型列表
func (s dictTypeService) QueryList(req *vo.QueryDictTypeListReq) ([]*model.SysDictType, int64, error) {
	var (
		u = dao.SysDictType
		d = u.Where()
	)
	if req.PageSize < 1 {
		req.PageSize = 10
	}
	if req.PageNum < 1 {
		req.PageNum = 1
	}
	if req.DictName != "" {
		d = d.Where(u.DictName.Like("%" + req.DictName + "%"))
	}
	if req.DictType != "" {
		d = d.Where(u.DictType.Like("%" + req.DictType + "%"))
	}
	if req.Status != "" {
		d = d.Where(u.Status.Eq(req.Status))
	}
	if req.Params != nil {
		if req.Params.BeginTime > 0 && req.Params.EndTime > 0 {
			d = d.Where(u.CreatedAt.Gte(req.Params.BeginTime))
			d = d.Where(u.CreatedAt.Lte(req.Params.EndTime))
		}
	}
	return d.FindByPage((req.PageNum-1)*req.PageSize, req.PageSize)
}

// All 查询所有字典类型
func (s dictTypeService) All() ([]*model.SysDictType, error) {
	return dao.SysDictType.Find()
}

// GetByID 根据dictId查询字典类型
func (s dictTypeService) GetByID(dictId int64) (*model.SysDictType, error) {
	return dao.SysDictType.Where(dao.SysDictType.DictID.Eq(dictId)).First()
}

// GetByDictTypeName 根据字典类型名查询
// dictName 字典名称
func (s dictTypeService) GetByDictTypeName(dictName string) (*model.SysDictType, error) {
	return dao.SysDictType.Where(dao.SysDictType.DictName.Eq(dictName)).First()
}

// ChangeStatus 修改字典类型状态
func (s dictTypeService) ChangeStatus(dictId int64, status string) error {
	_, err := dao.SysDictType.Where(dao.SysDictType.DictID.Eq(dictId)).Update(dao.SysDictType.Status, status)
	return err
}

// Add 添加字典类型
func (s dictTypeService) Add(req *vo.AddDictTypeReq) error {
	if !s.CheckDictTypeUnique(0, req.DictType) {
		return DictTypeNameUniqueError
	}
	u := dao.SysDictType
	newDictType := &model.SysDictType{
		DictName: req.DictName,
		DictType: req.DictType,
		Status:   req.Status,
		CreateBy: req.CreateBy,
		Remark:   req.Remark,
	}
	err := u.Create(newDictType)
	if err != nil {
		systemLog.Errorln(err.Error())
		return ServerError
	}
	return nil
}

// Update 修改字典类型
func (s dictTypeService) Update(req *vo.EditDictTypeReq) error {
	u := dao.SysDictType
	_, err := u.Where(u.DictID.Eq(req.DictId)).Updates(map[string]any{
		u.DictName.ColumnName().String(): req.DictName,
		u.DictType.ColumnName().String(): req.DictType,
		u.Status.ColumnName().String():   req.Status,
		u.Remark.ColumnName().String():   req.Remark,
		u.UpdateBy.ColumnName().String(): req.UpdateBy,
	})
	if err != nil {
		systemLog.Errorln(err.Error())
		return ServerError
	}
	return nil
}

// Delete 删除字典类型
func (s dictTypeService) Delete(dictIds []int64) error {
	_, err := dao.SysDictType.Where(dao.SysDictType.DictID.In(dictIds...)).Delete()
	return err
}

// CheckDictTypeUnique 检查字典类型是否已存在
func (s dictTypeService) CheckDictTypeUnique(dictId int64, dictType string) bool {
	if dictId == 0 {
		count, err := dao.SysDictType.Where(dao.SysDictType.DictType.Eq(dictType)).Count()
		if err != nil {
			systemLog.Errorln(err.Error())
			return true
		}
		return count < 1
	}
	dict, err := s.GetByID(dictId)
	if err != nil {
		systemLog.Errorln(err.Error())
		return true
	}
	if dict.DictType == dictType {
		return true
	}
	count, err := dao.SysDictType.Where(dao.SysDictType.DictType.Eq(dictType)).Count()
	if err != nil {
		systemLog.Errorln(err.Error())
		return true
	}
	return count < 1
}
